﻿using System;
using System.Reflection;
using System.Xml.Serialization;

namespace EnterpriseFramework.Configuration
{
    [Serializable]
    public struct Providers
    {
        public string Default { get; set; }
        [XmlElement("Provider")]
        public Provider[] ProviderList { get; set; }

        //public ICompatibility DefaultProvider { get; set; }

        //public T GetDefaultProvider<T>()
        //{
        //    foreach (Provider provider in this.ProviderList)
        //    {
        //        if (string.Compare(Default, provider.Name, true) == 0)
        //        {
        //            T compatibility = default(T);
        //            if (!string.IsNullOrEmpty(provider.ProviderClass) && string.IsNullOrEmpty(provider.ExternalAssembly))
        //                compatibility = (T)Activator.CreateInstance(Type.GetType(provider.ProviderClass));
        //            else if (!string.IsNullOrEmpty(provider.ExternalAssembly))
        //            {
        //                Assembly assembly = Assembly.LoadFrom(provider.ExternalAssembly);
        //                compatibility = (T)assembly.CreateInstance(provider.ProviderClass);
        //            }
        //            //compatibility.Connection.ConnectionString = provider.ConnectionString;
        //            return compatibility;
        //        }
        //    }
        //    return default(T);
        //}

        //public ICompatibility GetProvider(string ProviderName)
        //{
        //    foreach (Provider provider in this.ProviderInfo)
        //    {
        //        if (string.Compare(Default, ProviderName, true) == 0)
        //        {
        //            ICompatibility compatibility = null;
        //            if (!string.IsNullOrEmpty(provider.ProviderClass) && string.IsNullOrEmpty(provider.ExternalAssembly))
        //                compatibility = Activator.CreateInstance(Type.GetType(provider.ProviderClass)) as ICompatibility;
        //            else if (!string.IsNullOrEmpty(provider.ExternalAssembly))
        //            {
        //                Assembly assembly = Assembly.LoadFrom(provider.ExternalAssembly);
        //                compatibility = assembly.CreateInstance(provider.ProviderClass) as ICompatibility;
        //            }
        //            compatibility.Connection.ConnectionString = provider.ConnectionString;
        //            return compatibility;
        //        }
        //    }
        //    return default(ICompatibility);
        //}
    }

    [Serializable]
    public struct Provider
    {
        public string Name { get; set; }
        public string ExternalAssembly { get; set; }
        public string ProviderClass { get; set; }
        public string ConnectionString { get; set; }
    }
}
